<?php
/**
 * Created by PhpStorm.
 * User: Mak
 * Date: 2016/4/5 0005
 * Time: 2:02
 */
namespace Admin\Controller;

use Think\Controller;
use Think\Page;

class UserMagController extends Controller
{

    //管理员
    public function adminMag()
    {
        $AdminInfo = D("AdminInfo");
        $ret = $AdminInfo->select();
        $this->assign("ret", $ret);
        $this->display();
    }

    //教师
    public function tecMag()
    {
        $DeptId = I("get.deptid");
        $TecInfo = D("TecInfo");
        $p = empty($_GET['p']) ? 1 : $_GET['p'];
        if ($DeptId == 0) {
            $ret = $TecInfo->order("deptid,tecid")->page($p . ',10')->select();

            $count = $TecInfo->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);

            $this->assign("ret", $ret);
        } else {
            $ret = $TecInfo->order("deptid,tecid")->where("deptid=$DeptId")->page($p . ',10')->select();

            $count = $TecInfo->where("deptid=$DeptId")->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);
            $this->assign("ret", $ret);
        }
        $this->display();
    }

    //学生
    public function stuMag()
    {
        $DeptId = I("get.deptid");
        $StuInfo = D("StuInfo");
        if ($DeptId == 0) {
            $ret = $StuInfo->order("deptid,classid,stuid")->select();

            $count = $StuInfo->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);

            $this->assign("ret", $ret);
        } else {
            $ret = $StuInfo->order("deptid,classid,stuid")->where("deptid=$DeptId")->select();

            $count = $StuInfo->where("deptid=$DeptId")->count();
            $Page = new Page($count, 10);
            $Page->setConfig('prev', '<li class="waves-effect"><i class="mdi-navigation-chevron-left"></i></li>');
            $Page->setConfig('next', '<li class="waves-effect"><i class="mdi-navigation-chevron-right"></i></li>');
            $Page->setConfig('first', '<li class="waves-effect"><i class="material-icons">1</i></li>');
            $Page->setConfig('last', '<li class="waves-effect"><i class="material-icons">%TOTAL_PAGE%</i></li>');
            $Page->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %DOWN_PAGE% %END%');
            $show = $Page->show();
            $this->assign('page', $show);

            $this->assign("ret", $ret);
        }
        $this->display();
    }

    //添加学生帐号
    public function addStu()
    {
        if (IS_POST) {
            $StuInfo = D("StuInfo");
            if ($StuInfo->create()) {
                if ($StuInfo->add()) {
                    $this->success("添加成功", U("Admin/UserMag/stuMag"));
                } else {
                    $this->error("添加失败");
                }
            }
        } else {
            $this->display();

        }
    }

    //添加管理员帐号
    public function addAdmin()
    {
        if (IS_POST) {
            $AdminInfo = D("AdminInfo");
            if ($AdminInfo->create()) {
                if ($AdminInfo->add()) {
                    $this->success("添加成功", U("Admin/UserMag/adminMag"));
                } else {
                    $this->error("添加失败");
                }
            }
        } else {
            $this->display();

        }
    }

    //添加教师帐号
    public function addTec()
    {
        if (IS_POST) {
            $TecInfo = D("TecInfo");
            if ($TecInfo->create()) {
                if ($TecInfo->add()) {
                    $this->success("添加成功", U("Admin/UserMag/tecMag"));
                } else {
                    $this->error("添加失败");
                }
            }
        } else {
            $this->display();

        }

    }

    //删除学生帐号
    public function delStu()
    {

        $Id = I("get.id");
        $StuInfo = D("StuInfo");
        if ($StuInfo->where("id={$Id}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }

    }

    //删除管理员帐号
    public function delAdmin()
    {
        $Id = I("get.id");
        $AdminInfo = D("AdminInfo");
        if ($AdminInfo->where("id={$Id}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }

    }

    //删除教师帐号
    public function delTec()
    {

        $Id = I("get.id");
        $TecInfo = D("TecInfo");
        if ($TecInfo->where("id={$Id}")->delete()) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }

    }

    //批量导入学生
    public function importStu()
    {
        import("Org.Util.PHPExcel");
        $StuInfo = D("StuInfo");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["stu"]["savepath"] . $upload["stu"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $sheet = $PHPExcel->getSheet(0);
        foreach ($sheet->getRowIterator() as $row) {
            if ($row->getRowIndex() < 2) {
                continue;
            }
            $str = '';
            echo "";
            foreach ($row->getCellIterator() as $cell) {
                $str .= $cell->getValue() . '|*|';
            }
            $strs = explode("|*|", $str);
            $post["stuid"] = $strs[0];
            $post["password"] = $strs[1];
            $post["deptid"] = getDeptId($strs[2]);
            $post["classid"] = getClassId($strs[3]);
            $post["name"] = getDeptId($strs[4]);
            $post["sex"] = $strs[5];
            $ret = $StuInfo->add($post, array(), true);
            if ($ret) {
                $cnt += 1;
            } else {
                $this->error("文件中存在错误");
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

    //批量导入管理员
    public function importAdmin()
    {

    }

    //批量导入教师
    public function importTec()
    {
        import("Org.Util.PHPExcel");
        $TecInfo = D("TecInfo");
        $cnt = 0;
        $upload = use_upload();
        $file = "./UPLOADS/" . $upload["tec"]["savepath"] . $upload["tec"]["savename"];
        $fileType = \PHPExcel_IOFactory::identify($file);
        $Reader = \PHPExcel_IOFactory::createReader($fileType);
        $PHPExcel = $Reader->load($file);
        $sheet = $PHPExcel->getSheet(0);
        foreach ($sheet->getRowIterator() as $row) {
            if ($row->getRowIndex() < 2) {
                continue;
            }
            $str = '';
            echo "";
            foreach ($row->getCellIterator() as $cell) {
                $str .= $cell->getValue() . '|*|';
            }
            $strs = explode("|*|", $str);
            $post["tecid"] = $strs[0];
            $post["password"] = $strs[1];
            $post["name"] = $strs[2];
            $post["deptid"] = getDeptId($strs[3]);
            $post["rank"] = getRankId($strs[4]);
//            dump($strs);
            $ret = $TecInfo->add($post, array(), true);
            if ($ret) {
                $cnt += 1;
            } else {
                $this->error("文件中存在错误");
            }
        }
        $this->success("有" . $cnt . "条数据导入成功");
    }

//修改学生信息
    public function editStu()
    {
        $StuInfo = D("StuInfo");
        if (IS_POST) {
            $StuInfo->create();
            if ($a = $StuInfo->save()) {
                $this->success("修改成功", U("Admin/UserMag/stuMag"));
            } else {
                $this->error("修改失败");
            }
        } else {
            $Id = I("get.id");
            $ret = $StuInfo->where("id=$Id")->find();
            $this->assign("ret", $ret);
            $this->display();
        }
    }

//修改管理员信息
    public function editAdmin()
    {
        $AdminInfo = D("AdminInfo");
        if (IS_POST) {
            $AdminInfo->create();
            if ($a = $AdminInfo->save()) {
                $this->success("修改成功", U("Admin/UserMag/adminMag"));
            } else {
                $this->error("修改失败");
            }
        } else {
            $Id = I("get.id");
            $ret = $AdminInfo->where("id=$Id")->find();
            $this->assign("ret", $ret);
            $this->display();
        }
    }

//修改教师信息
    public function editTec()
    {
        $TecInfo = D("TecInfo");
        if (IS_POST) {
            $TecInfo->create();
            if ($a = $TecInfo->save()) {
                $this->success("修改成功", U("Admin/UserMag/tecMag"));
            } else {
                $this->error("修改失败");
            }
        } else {
            $Id = I("get.id");
            $ret = $TecInfo->where("id=$Id")->find();
            $this->assign("ret", $ret);
            $this->display();
        }
    }

    public function retClassName()
    {
        $DeptId = I("post.deptid");
        $ClassInfo = D("ClassInfo");
        $ret = $ClassInfo->where("did=$DeptId")->order("cid")->select();
        $this->ajaxReturn($ret);
    }

    //导出教师信息
    public function outputTec()
    {
        import("Org.Util.PHPExcel");
        $TecInfo = D("TecInfo");
        $ret = $TecInfo->order("deptid,tecid")->select();
        $PHPExcel = new \PHPExcel();
        $Sheet = $PHPExcel->getActiveSheet();
        $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
        $Sheet->getDefaultStyle()
            ->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $Sheet->getColumnDimension('A')->setWidth(5);
        $Sheet->getColumnDimension('B')->setWidth(10);
        $Sheet->getColumnDimension('C')->setWidth(30);
        $Sheet->getColumnDimension('D')->setWidth(30);
        $Sheet->getColumnDimension('E')->setWidth(30);
        $Sheet->setCellValue("A1", "序号")
            ->setCellValue("B1", "教师编号")
            ->setCellValue("C1", "系部名称")
            ->setCellValue("D1", "教师姓名")
            ->setCellValue("E1", "权限")
            ->getStyle("A1:E1")
            ->getFont()
            ->setName("微软雅黑")
            ->setSize(14)
            ->setBold(true);
        $startcell = 2;
        $i = 1;
        foreach ($ret as $rows) {
            $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->setCellValue("A" . $startcell, $i++)
                ->setCellValueExplicit("B" . $startcell, $rows["tecid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue("C" . $startcell, getDeptName($rows["deptid"]))
                ->setCellValue("D" . $startcell, $rows["name"])
                ->setCellValue("E" . $startcell, getRankName($rows["rank"]));
            $startcell++;
        }

        $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
        browser_export("教师信息表");
        $obj->save("php://output");
    }

    //导出学生信息
    public function outputStu()
    {
        import("Org.Util.PHPExcel");
        $StuInfo = D("StuInfo");
        $ret = $StuInfo->order("deptid,stuid")->select();
        $PHPExcel = new \PHPExcel();
        $Sheet = $PHPExcel->getActiveSheet();
        $Sheet->getDefaultStyle()->getFont()->setName("宋体")->setSize(14);
        $Sheet->getDefaultStyle()
            ->getAlignment()
            ->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER)
            ->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $Sheet->getColumnDimension('A')->setWidth(5);
        $Sheet->getColumnDimension('B')->setAutoSize(true);
        $Sheet->getColumnDimension('C')->setWidth(30);
        $Sheet->getColumnDimension('D')->setWidth(30);
        $Sheet->getColumnDimension('E')->setWidth(30);
        $Sheet->setCellValue("A1", "序号")
            ->setCellValue("B1", "学号")
            ->setCellValue("C1", "系部名称")
            ->setCellValue("D1", "班级")
            ->setCellValue("E1", "姓名")
            ->getStyle("A1:E1")
            ->getFont()
            ->setName("微软雅黑")
            ->setSize(14)
            ->setBold(true);
        $startcell = 2;
        $i = 1;
        foreach ($ret as $rows) {
            $Sheet->getStyle("B" . $startcell)->getNumberFormat()->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_TEXT);
            $Sheet->setCellValue("A" . $startcell, $i++)
                ->setCellValueExplicit("B" . $startcell, $rows["stuid"], \PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValue("C" . $startcell, getDeptName($rows["deptid"]))
                ->setCellValue("D" . $startcell, getClassName($rows["classid"]))
                ->setCellValue("E" . $startcell, $rows["name"]);
            $startcell++;
        }

        $obj = \PHPExcel_IOFactory::createWriter($PHPExcel, "Excel5");
        browser_export("学生信息表");
        $obj->save("php://output");
    }
}